﻿using Klear.Library.Models;
using SQLite;

namespace Klear.Library.Services;

public class BudgetService : IBudgetService
{
    private readonly IDatabaseService _databaseService;
    
    public BudgetService(IDatabaseService databaseService)
    {
        _databaseService = databaseService;
    }

    public async Task<BudgetModel> LoadBudgetSettingsAsync()
    {
        // 从数据库获取预算设置
        return await _databaseService.GetBudgetSettingsAsync();
    }

    public async Task SaveBudgetSettings(BudgetModel budgetModel)
    {
        // 更新数据库中的预算设置
        await _databaseService.UpdateBudgetSettingsAsync(budgetModel);
    }

    public async Task<decimal> CalculateActualSpending(DateTime startDate, DateTime endDate)
    {
        // 获取指定时间段内的实际支出总和
        return await _databaseService.GetActualSpendingAsync(startDate, endDate);
    }
    
    public async Task<bool> HasOverlappingBudget(DateTime startDate, DateTime endDate, int? excludeId = null)
    {
        return await _databaseService.HasOverlappingBudget(startDate, endDate, excludeId);
    }
    
    public async Task<List<BudgetModel>> GetCurrentAndFutureBudgetsAsync()
    {
        return await _databaseService.GetCurrentAndFutureBudgetsAsync();
    }

    public async Task DeleteBudgetAsync(BudgetModel budget)
    {
        await _databaseService.DeleteBudgetAsync(budget);
    }
    
    public async Task<BudgetModel> GetCurrentMonthBudgetAsync()
    {
        return await _databaseService.GetCurrentMonthBudgetAsync();
    }
}